package giit.computerbase.server;

import giit.computerbase.dao.SQL;
import giit.computerbase.dao.SQLFactory;
import giit.computerbase.util.Application;
import giit.computerbase.util.Hash;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

/**
 * User: tum0r
 * Package Name: giit.computerbase.server
 * Time: 2018-11-28 Wednesday 19:42:45
 */
public class SearchServer {
    private SQL sql;

    public SearchServer() {
        try {
            sql = SQLFactory.getSQL();
            sql.open((String) Application.getConfig().databaseConfig.get("address"),
                    (int) Application.getConfig().databaseConfig.get("port"),
                    Application.DATABASE_NAME,
                    (String) Application.getConfig().databaseConfig.get("encode"),
                    (String) Application.getConfig().databaseConfig.get("user"),
                    (String) Application.getConfig().databaseConfig.get("password"));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public ArrayList<HashMap<String, Object>> search(String title) {
        StringBuffer temp = new StringBuffer();
        temp.append("%");
        for (int i = 0; i < title.length(); i++) {
            temp.append(title.charAt(i));
            temp.append("%");
        }
        ArrayList<HashMap<String, Object>> result = sql.query("select * from activity_information where title like ?", new Object[]{temp.toString()});
        ArrayList<HashMap<String, Object>> resultTemp = sql.query("select * from activity_information where main like ?", new Object[]{temp.toString()});
        Iterator<HashMap<String, Object>> iterator = resultTemp.iterator();
        HashMap<String, Object> main;
        while (iterator.hasNext()) {
            main = iterator.next();
            if (!result.contains(main)) {
                result.add(main);
            }
        }
        return result;
    }
}
